import 'package:json_annotation/json_annotation.dart';

part 'coin_product.g.dart';

@JsonSerializable()
class CoinProduct {
  final String productId;
  final int coins;
  final double price;
  final String name;
  final bool isPromo;
  final String description;

  const CoinProduct({
    required this.productId,
    required this.coins,
    required this.price,
    required this.name,
    required this.isPromo,
    required this.description,
  });

  factory CoinProduct.fromJson(Map<String, dynamic> json) => _$CoinProductFromJson(json);
  Map<String, dynamic> toJson() => _$CoinProductToJson(this);
  
  // 静态产品列表，严格按照StoreKit配置文件定义
  static const List<CoinProduct> allProducts = [
    // 常规商品 (470600-470607)
    CoinProduct(
      productId: '470600',
      coins: 100,
      price: 0.99,
      name: '100 coins',
      isPromo: false,
      description: 'Purchase 100 coins for learning',
    ),
    CoinProduct(
      productId: '470601',
      coins: 299,
      price: 2.99,
      name: '299 coins',
      isPromo: false,
      description: 'Purchase 299 coins for learning',
    ),
    CoinProduct(
      productId: '470602',
      coins: 399,
      price: 3.99,
      name: '399 coins',
      isPromo: false,
      description: 'Purchase 399 coins for learning',
    ),
    CoinProduct(
      productId: '470603',
      coins: 499,
      price: 4.99,
      name: '499 coins',
      isPromo: false,
      description: 'Purchase 499 coins for learning',
    ),
    CoinProduct(
      productId: '470604',
      coins: 1199,
      price: 9.99,
      name: '1199 coins',
      isPromo: false,
      description: 'Purchase 1199 coins for learning',
    ),
    CoinProduct(
      productId: '470605',
      coins: 2499,
      price: 19.99,
      name: '2499 coins',
      isPromo: false,
      description: 'Purchase 2499 coins for learning',
    ),
    CoinProduct(
      productId: '470606',
      coins: 6999,
      price: 49.99,
      name: '6999 coins',
      isPromo: false,
      description: 'Purchase 6999 coins for learning',
    ),
    CoinProduct(
      productId: '470607',
      coins: 14999,
      price: 99.99,
      name: '14999 coins',
      isPromo: false,
      description: 'Purchase 14999 coins for learning',
    ),
    
    // 促销商品 (470608-470612)
    CoinProduct(
      productId: '470608',
      coins: 499,
      price: 1.99,
      name: '499 coins (Promo)',
      isPromo: true,
      description: 'Special offer: 499 coins for learning',
    ),
    CoinProduct(
      productId: '470609',
      coins: 1199,
      price: 4.99,
      name: '1199 coins (Promo)',
      isPromo: true,
      description: 'Special offer: 1199 coins for learning',
    ),
    CoinProduct(
      productId: '470610',
      coins: 2499,
      price: 11.99,
      name: '2499 coins (Promo)',
      isPromo: true,
      description: 'Special offer: 2499 coins for learning',
    ),
    CoinProduct(
      productId: '470611',
      coins: 6999,
      price: 34.99,
      name: '6999 coins (Promo)',
      isPromo: true,
      description: 'Special offer: 6999 coins for learning',
    ),
    CoinProduct(
      productId: '470612',
      coins: 14999,
      price: 79.99,
      name: '14999 coins (Promo)',
      isPromo: true,
      description: 'Special offer: 14999 coins for learning',
    ),
  ];
  
  // 获取常规商品
  static List<CoinProduct> get regularProducts => allProducts.where((p) => !p.isPromo).toList();
  
  // 获取促销商品
  static List<CoinProduct> get promoProducts => allProducts.where((p) => p.isPromo).toList();
  
  // 根据产品ID查找商品
  static CoinProduct? findByProductId(String productId) {
    try {
      return allProducts.firstWhere((p) => p.productId == productId);
    } catch (e) {
      return null;
    }
  }
  
  // 获取最优惠的促销商品
  static CoinProduct? get bestPromoProduct {
    if (promoProducts.isEmpty) return null;
    
    double bestValue = 0;
    CoinProduct? bestProduct;
    
    for (final product in promoProducts) {
      final value = product.coins / product.price;
      if (value > bestValue) {
        bestValue = value;
        bestProduct = product;
      }
    }
    
    return bestProduct;
  }
}

@JsonSerializable()
class VipPlan {
  final String planId;
  final String name;
  final int coinsCost;
  final int durationDays;
  final String displayDuration;
  final String description;
  final List<String> features;
  final bool isPopular;
  final String badge;
  final double valuePerDay;

  const VipPlan({
    required this.planId,
    required this.name,
    required this.coinsCost,
    required this.durationDays,
    required this.displayDuration,
    required this.description,
    required this.features,
    required this.isPopular,
    required this.badge,
    required this.valuePerDay,
  });

  factory VipPlan.fromJson(Map<String, dynamic> json) => _$VipPlanFromJson(json);
  Map<String, dynamic> toJson() => _$VipPlanToJson(this);

  // VIP订阅计划列表
  static const List<VipPlan> allPlans = [
    VipPlan(
      planId: 'vip_1day',
      name: '1 Day VIP',
      coinsCost: 50,
      durationDays: 1,
      displayDuration: '1 Day',
      description: 'Try VIP for one day',
      features: [
        'Unlimited daily quizzes',
      ],
      isPopular: false,
      badge: 'TRY',
      valuePerDay: 50.0, // 50/1
    ),
    VipPlan(
      planId: 'vip_7days',
      name: '7 Days VIP',
      coinsCost: 300,
      durationDays: 7,
      displayDuration: '7 Days',
      description: 'One week of unlimited testing',
      features: [
        'Unlimited daily quizzes',
      ],
      isPopular: false,
      badge: '',
      valuePerDay: 42.86, // 300/7
    ),
    VipPlan(
      planId: 'vip_1month',
      name: '1 Month VIP',
      coinsCost: 800,
      durationDays: 30,
      displayDuration: '1 Month',
      description: 'Perfect for regular practice',
      features: [
        'Unlimited daily quizzes',
      ],
      isPopular: true,
      badge: 'POPULAR',
      valuePerDay: 26.67, // 800/30
    ),
    VipPlan(
      planId: 'vip_3months',
      name: '3 Months VIP',
      coinsCost: 2100,
      durationDays: 90,
      displayDuration: '3 Months',
      description: 'Great value for consistent learners',
      features: [
        'Unlimited daily quizzes',
      ],
      isPopular: false,
      badge: '',
      valuePerDay: 23.33, // 2100/90
    ),
    VipPlan(
      planId: 'vip_6months',
      name: '6 Months VIP',
      coinsCost: 3600,
      durationDays: 180,
      displayDuration: '6 Months',
      description: 'Best value for serious learners',
      features: [
        'Unlimited daily quizzes',
      ],
      isPopular: false,
      badge: 'BEST VALUE',
      valuePerDay: 20.0, // 3600/180
    ),
    VipPlan(
      planId: 'vip_12months',
      name: '1 Year VIP',
      coinsCost: 6000,
      durationDays: 365,
      displayDuration: '1 Year',
      description: 'Maximum savings for dedicated learners',
      features: [
        'Unlimited daily quizzes',
      ],
      isPopular: false,
      badge: 'SAVE 50%',
      valuePerDay: 16.44, // 6000/365
    ),
    VipPlan(
      planId: 'vip_lifetime',
      name: 'Lifetime VIP',
      coinsCost: 15000,
      durationDays: -1, // -1 表示永久
      displayDuration: 'Lifetime',
      description: 'One-time payment, lifetime access',
      features: [
        'Unlimited daily quizzes',
      ],
      isPopular: false,
      badge: 'LIFETIME',
      valuePerDay: 0, // 一次性付费
    ),
  ];

  // 获取所有计划
  static List<VipPlan> get getAllPlans => allPlans;

  // 根据计划ID查找
  static VipPlan? findByPlanId(String planId) {
    try {
      return allPlans.firstWhere((p) => p.planId == planId);
    } catch (e) {
      return null;
    }
  }

  // 获取最受欢迎的计划
  static VipPlan? get popularPlan {
    try {
      return allPlans.firstWhere((p) => p.isPopular);
    } catch (e) {
      return null;
    }
  }

  // 获取最佳价值计划
  static VipPlan? get bestValuePlan {
    return allPlans.where((p) => p.durationDays > 0).reduce((a, b) => 
      a.valuePerDay < b.valuePerDay ? a : b
    );
  }
} 